<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('修改并卷')" />
    <th:block th:include="include :: datetimepicker-css" />
    <th:block th:include="include :: bootstrap-fileinput-css" />
    <th:block th:include="include :: bootstrap-fileinput-js" />
</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-ribbon-edit" th:object="${processRibbon}">
            <input name="sign" value="1" type="hidden">
            <input name="id" th:field="*{id}" type="hidden">
            <div class="form-group">
                <label class="col-sm-3 control-label is-required">合并材料编号：</label>
                <div class="col-sm-8">
                    <input name="ribbonNumber" disabled th:field="*{ribbonNumber}" class="form-control" type="text">
                </div>
            </div>
            <div class="form-group">
                <div class="col-sm-12">
                    <input type="hidden" name="inventoryIds" id="inventoryIds"/>
                    <label class="col-sm-3 control-label">原膜编号：</label>
                    <div class="col-sm-8">
                        <!--<button type="button" class="btn btn-white btn-sm" onclick="addColumn()"><i class="fa fa-plus"> 增加</i></button>-->
                        <!--<button type="button" class="btn btn-white btn-sm" onclick="delColumn()"><i class="fa fa-minus"> 删除</i></button>-->
                    </div>
                    <div class="col-sm-12 table-striped">
                        <table id="bootstrap-table"></table>
                    </div>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">镀铝面：</label>
                <div class="col-sm-8">
                    <select name="surface" class="form-control m-b" th:with="type=${@dict.getType('process_ribbon_state')}">
                        <option value="">请选择</option>
                        <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}" th:field="*{surface}"></option>
                    </select>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">质量文字反馈：</label>
                <div class="col-sm-8">
                    <input name="qualityState" th:field="*{qualityState}" class="form-control" type="text">
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">质量图片反馈：</label>
                <div class="col-sm-4">
                    <input id="qualityImg" name="file" multiple type="file" data-show-caption="true">
                </div>
                <div class="col-sm-2">
                    <a class="btn btn-success" onclick='sendFileImg("qualityImg")'><i class="fa fa-plus">图片上传</i></a>
                </div>
                <div class="col-sm-1">
                    <a class="btn btn-warning" onclick='removeImg("qualityImg")'><i class="fa fa-plus">清空图片</i></a>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label"></label>
                <div class="col-sm-8">
                    <div class="file-preview qualityImg" style="height: auto">
                    </div>
                    <input name="qualityImg" th:field="*{qualityImg}" type="hidden">
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">质量视频反馈：</label>
                <div class="col-sm-6">
                    <input id="qualityVideo" name="file" multiple type="file" data-show-caption="true">
                </div>
                <div class="col-sm-1">
                    <a class="btn btn-success" onclick='sendFile("qualityVideo")'><i class="fa fa-plus">视频上传</i></a>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label"></label>
                <div class="col-sm-8">
                    <div class="file-preview qualityVideo" style="height: auto">
                    </div>
                    <input name="qualityVideo" th:field="*{qualityVideo}" type="hidden">
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">操作起始时间：</label>
                <div class="col-sm-8">
                    <div class="input-group date">
                        <input name="startTime" id="startTime" autocomplete="off" th:value="${#dates.format(processRibbon.startTime, 'yyyy-MM-dd HH:mm:ss')}"
                               data-type="datetime" data-sign=1 class="form-control time-input" data-format="yyyy-MM-dd HH:mm:ss" placeholder="yyyy-MM-dd HH:mm:ss" type="text">
                        <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
                    </div>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">操作结束时间：</label>
                <div class="col-sm-8">
                    <div class="input-group date">
                        <input name="endTime" id="endTime" autocomplete="off" th:value="${#dates.format(processRibbon.endTime, 'yyyy-MM-dd HH:mm:ss')}"
                               data-type="datetime" data-sign=2 class="form-control time-input" data-format="yyyy-MM-dd HH:mm:ss" placeholder="yyyy-MM-dd HH:mm:ss" type="text">
                        <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
                    </div>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">耗时：</label>
                <div class="col-sm-8">
                    <input id="consumeTimeBck" th:field="*{consumeTime}" disabled class="form-control" type="text">
                    <input name="consumeTime" id="consumeTime" th:field="*{consumeTime}" class="form-control" type="hidden">
                </div>
            </div>
            <div class="form-group">
                <input type="hidden" name="operatingEquipment" th:field="*{operatingEquipment}" id="equaipment"/>
                <label class="col-sm-3 control-label">操作设备：</label>
                <div class="col-sm-8">
                    <select class="form-control" id="operat-select" onchange="chengeStatus(this.value)">
                        <option value="">请选择</option>
                        <option value="一号复卷机">一号复卷机</option>
                        <option value="二号复卷机">二号复卷机</option>
                        <option value="三号复卷机">三号复卷机</option>
                        <option value="四号复卷机">四号复卷机</option>
                        <option value="其他">其他</option>
                    </select>
                    <input id="other" placeholder="输入设备" class="form-control" type="hidden">
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label is-required">操作班次：</label>
                <div class="col-sm-8">
                    <input name="operatingNumber" th:field="*{operatingNumber}" class="form-control" type="text" >
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label is-required">操作人：</label>
                <div class="col-sm-8">
                    <!--<input name="operator" class="form-control" type="text" required>-->
                    <select name="operator" id="operator" class="form-control" data-none-selected-text="请选择">
                        <option value="">请选择</option>
                        <option th:each="user : ${users}" th:text="${user.userName}" th:value="${user.userName}"></option>
                    </select>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">并卷日期：</label>
                <div class="col-sm-8">
                    <div class="input-group date">
                        <input name="ribbonTime" th:value="${#dates.format(processRibbon.ribbonTime, 'yyyy-MM-dd')}"
                               class="form-control time-input" placeholder="yyyy-MM-dd" type="text">
                        <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
                    </div>
                </div>
            </div>
        </form>
    </div>
    <th:block th:include="include :: footer" />
    <!--<th:block th:include="include :: datetimepicker-js" />-->
    <script th:src="@{/js/jquery.tmpl.js}"></script>
    <script th:inline="javascript">
        var prefix = ctx + "process/ribbon";
        var imgs = [];
        $("#form-ribbon-edit").validate({
            focusCleanup: true
        });
        function getSuffix(url){
            var indexOf = url.indexOf(".")+1;
            return url.slice(indexOf,url.length);
        }
        $(function () {
            var ribbon = [[${processRibbon}]];
            $("#operator option[value= " + ribbon.operator + "]").prop("selected",true);
            if(ribbon.qualityVideo!=null && ribbon.qualityVideo!=""){
                if(getSuffix(ribbon.qualityVideo)=="mp4"){
                    $(".qualityVideo").html('<video src="'+ribbon.qualityVideo+'" controls="controls" style="height: 170px;width: 100%">');
                }else{
                    $(".qualityVideo").html('<img src="'+ribbon.qualityVideo+'" style="height: 100%;width: 100%">');
                }
            }
            if(ribbon.qualityImg!=null && ribbon.qualityImg!=""){
                var p = "";
                var split = ribbon.qualityImg.split(",");
                for (var i = 0; i < split.length; i++) {
                    imgs.push(split[i]);
                    p = p+'<img src="'+split[i]+'" style="height: 100%;width: 100%">';
                }
                $(".qualityImg").html(p);
            }
            var equipment = ribbon.operatingEquipment;
            if(equipment!=null && equipment!=''){
                if(equipment == "一号复卷机" || equipment == "二号复卷机"
                    || equipment == "三号复卷机" || equipment == "四号复卷机"){
                    $("#operat-select option[value= " + equipment + "]").prop("selected",true);
                }else{
                    $("#operat-select option[value= '其他']").prop("selected",true);
                    $("#other").attr("type","text");
                    $("#other").val(equipment);
                }
            }
        });

        function submitHandler() {
            var select = $("#operat-select").val();
            if(select == "其他"){
                $("#equaipment").val($("#other").val());
            }else{
                $("#equaipment").val(select);
            }
            if ($.validate.form()) {
                $.operate.save(prefix + "/edit", $('#form-ribbon-edit').serialize());
            }
        }
        function getReduce() {
            var startTime = $("#startTime").val();
            var endTime = $("#endTime").val();
            if(startTime!=null && startTime!=''
                && endTime!=null && endTime!=''){
                var start = new Date(startTime).getTime();
                var end = new Date(endTime).getTime();
                var reduce = end - start;
                if(reduce<0){
                    layer.alert("请选择正确的时间段");
                    $("#startTime").val("");
                    $("#endTime").val("");
                    $("#consumeTime").val("");
                    return false;
                }
                var min = Math.floor(reduce/(60*1000));
                $("#consumeTime").val(min);
            }
        }
        //多个图片上传
        function sendFileImg(name) {
            var formData = new FormData();
            //注入 name=file
            var files = $('#'+name)[0].files;
            if(files.length>10){
                layer.alert("上传上限位10");
                return false;
            }
            for (var i = 0; i < files.length; i++) {
                //注意：这里append进去的是File对象，而不是FileList对象
                formData.append("file", files[i]);
            }

            //  var formData = new FormData($('#'+name)[0]);
            // pp.append("file", val);
            $.ajax({
                type: "POST",
                url: ctx + "common/uploadNew",
                data: formData,
                processData: false,
                contentType: false,
                success: function(result) {
                    if (result!=null && result.code == web_status.SUCCESS) {
                        var a = result.url;
                        var p ="";
                        for (var i = 0; i < a.length; i++) {
                            imgs.push(a[i]);
                        }
                        for (var j = 0; j < imgs.length; j++) {
                            p=p+'<img src="'+imgs[j]+'" style="height: 100%;width: 100%">';
                        }
                        $('.'+name).html(p);
                        $('[name="'+name+'"]').val(imgs.join(","));

                    } else {
                        $.modal.alertError("上传失败");
                    }
                },
                error: function(error) {
                    $.modal.alertWarning("图片上传失败");
                }
            });
        }
        function removeImg(name) {
            imgs = [];
            $('.'+name).html("");
            $('#'+name).val("");
            $('[name="'+name+'"]').val("");
        }
        // 上传文件
        function sendFile(name) {
            var val = $('#'+name)[0].files;
            var indexOf = val[0].name.indexOf(".")+1;
            var s = val[0].name.slice(indexOf,val[0].name.length);
            var data = new FormData();
            data.append("file", val[0]);
            $.ajax({
                type: "POST",
                url: ctx + "common/upload",
                data: data,
                cache: false,
                contentType: false,
                processData: false,
                dataType: 'json',
                success: function(result) {
                    if (result!=null && result.code == web_status.SUCCESS) {
                        if(s=="mp4"){
                            $('.'+name).html('<video src="'+result.url+'" controls="controls" style="height: 170px;width: 100%">');
                            $('[name="'+name+'"]').val(result.url);
                        }else{
                            $('.'+name).html('<img src="'+result.url+'" style="height: 100%;width: 100%">');
                            $('[name="'+name+'"]').val(result.url);
                        }
                    } else {
                        $.modal.alertError("上传失败");
                    }
                },
                error: function(error) {
                    $.modal.alertWarning("图片上传失败");
                }
            });
        }
        function chengeStatus(val) {
            if(val == "其他"){
                $("#other").attr("type","text");
            }else{
                $("#other").attr("type","hidden");
            }
        }
        //原膜列表
        var data = [[${inventorys}]];
        $(function() {
            var options = {
                pagination: false,
                showSearch: false,
                showRefresh: false,
                showToggle: false,
                showColumns: false,
                columns: [{
                    checkbox: true
                },
                {
                    field: 'id',
                    title: 'id',
                    visible: false
                },
                {
                    field: 'number',
                    align: 'center',
                    title: '原膜编号',
                    formatter: function(value, row, index) {
                        // var obj = {};
                        // obj.index = index;
                        // obj.list = data;
                        // return $("#goodsTypeTpl").tmpl(obj).html();
                        var html = $.common.sprintf("<input class='form-control' disabled type='text' name='goods[%s].number' value='%s'>", index, value);
                        return html;
                    }
                },
                {
                    field: 'factoryName',
                    align: 'center',
                    title: '厂家',
                    formatter: function(value, row, index) {
                        var html = $.common.sprintf("<input class='form-control' name='goods[%s].factoryName' disabled type='text' value='%s'>", index, value);
                        return html;
                    }
                },
                {
                    field: 'materialsName',
                    align: 'center',
                    title: '材料名称',
                    formatter: function(value, row, index) {
                        var html = $.common.sprintf("<input class='form-control' name='goods[%s].materialsName' disabled type='text' value='%s'>", index, value);
                        return html;
                    }
                },
                {
                    field: 'color',
                    align: 'center',
                    title: '颜色',
                    formatter: function(value, row, index) {
                        var html = $.common.sprintf("<input class='form-control' name='goods[%s].color' disabled type='text' value='%s'>", index, value);
                        return html;
                    }
                },
                {
                    field: 'mDate',
                    align: 'center',
                    title: '来料日期',
                    formatter: function(value, row, index) {
                        var html = $.common.sprintf("<input class='form-control' name='goods[%s].mDate' disabled type='text' value='%s'>", index, value);
                        return html;
                    }
                }]
            };
            $.table.init(options);
            show();
        });
        //列表回显
        function show() {
            var table1 = $("#" + table.options.id).bootstrapTable('getData');
            var count = table1.length;
            for (var i = 0; i < data.length; i++) {
                var obj = data[i];
                $("#" + table.options.id).bootstrapTable('insertRow', {
                    index: count,
                    row: {
                        index: $.table.serialNumber(count),
                        id:obj.id,
                        number:obj.number,
                        factoryName: obj.factoryName,
                        materialsName: obj.materialsName,
                        color: obj.color,
                        mDate: obj.mDate,
                    }
                });
            }
        }
    </script>
</body>
</html>
